package com.example.demo.utils;


import com.example.demo.StreamGobble;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

public class ExcuteLinux {

    public static void exeCmd(String commandStr) {

//        String result = null;
        try {
            String[] cmd = new String[]{"/bin/sh","-c",commandStr};
            Process ps = Runtime.getRuntime().exec(cmd);
            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));

//            StringBuffer sb = new StringBuffer();
//            String line;

//            while ((line = br.readLine()) != null) {
//                //执行结果加上回车
//                sb.append(line).append("\n");
//            }
//            result = sb.toString();

            String line = null;
            while ((line = br.readLine()) != null){
                System.out.println(line);
            }
//            new StreamGobble(ps.getInputStream(), "").start();
//            new StreamGobble(ps.getErrorStream(), "ERROR").start();
//            ps.waitFor();

        } catch (Exception e) {
            e.printStackTrace();
        }
//        return result;
    }
    public static void exeCmdSim(String commandStr, String parentDir, File directory, String dataTarget) {
//        int res = Integer.MAX_VALUE;
//        String result = null;
        StreamGobble stream = null;
        try {
            String[] cmd = new String[]{"/bin/sh","-c",commandStr};
            Process ps = Runtime.getRuntime().exec(cmd);
//            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));

//            StringBuffer sb = new StringBuffer();
//            String line;
//            while ((line = br.readLine()) != null) {
//                //执行结果加上回车
//                sb.append(line).append("\n");
//            }
//            result = sb.toString();

//            String line = null;
//            while ((line = br.readLine()) != null){
//                System.out.println(line);
//            }
            stream = new StreamGobble(ps.getInputStream(),parentDir);

            stream.start();
//            if(!stream.isAlive()){
//                System.out.println(parentDir+"仿真成功");
//                exeCmd("cd /home/lenovo/java/data"+"/"+parentDir +" && "+"zip -r "+parentDir+".zip"+" "+"outputfiles");
//                System.out.println(parentDir+"输出数据压缩完成");
//                exeCmd("cp -a "+directory.getParentFile()+"/"+parentDir+".zip"+" "+dataTarget);
//            }
//            new StreamGobble(ps.getErrorStream(), "ERROR").start();
//            res = ps.waitFor();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static StreamGobble exeCmdStream(String commandStr,StreamGobble stream) {
        StreamGobble curStream = null;
        try {
            String[] cmd = new String[]{"/bin/sh","-c",commandStr};
            Process ps = Runtime.getRuntime().exec(cmd);
//            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));

//            StringBuffer sb = new StringBuffer();
//            String line;
//            while ((line = br.readLine()) != null) {
//                //执行结果加上回车
//                sb.append(line).append("\n");
//            }
//            result = sb.toString();

//            String line = null;
//            while ((line = br.readLine()) != null){
//                System.out.println(line);
//            }
            curStream = new StreamGobble(ps.getInputStream(), "");
            stream.join();
            curStream.start();
//            new StreamGobble(ps.getErrorStream(), "ERROR").start();
//            ps.waitFor();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return curStream;
    }
}

